package com.mars.foodserver.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.foodserver.constant.Constant;
import com.mars.foodserver.entity.SysUser;
import com.mars.foodserver.entity.User;
import com.mars.foodserver.exception.WorkException;
import com.mars.foodserver.mapper.SysUserMapper;
import com.mars.foodserver.request.SysUserRequest;
import com.mars.foodserver.response.UserInfoResponse;
import com.mars.foodserver.response.UserResponse;
import com.mars.foodserver.response.base.PageInfo;
import com.mars.foodserver.service.ISysUserService;
import com.mars.foodserver.utils.GeneratorUtil;
import com.mars.foodserver.utils.JwtUtil;
import com.mars.foodserver.utils.RedisUtils;
import com.mars.foodserver.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 运营后台用户表 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-07-19
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private TokenUtils tokenUtils;


    @Override
    public UserResponse login(SysUserRequest request) {
        SysUser sysUser = baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().
                eq(SysUser::getMobile, request.getMobile()));
        if (Objects.isNull(sysUser)) {
            throw new WorkException("你还未注册哦");
        }
        if (!sysUser.getPassword().equals(request.getPassword())) {
            throw new WorkException("账号密码不正确");
        }
        //创建令牌
        String token = createToken(sysUser);
        UserResponse response = new UserResponse();
        response.setToken(token);
        UserInfoResponse infoResponse = new UserInfoResponse();
        infoResponse.setUserId(sysUser.getId());
        infoResponse.setNickname(sysUser.getNickname());
        infoResponse.setAvatar(sysUser.getAvatar());
        response.setUserInfoResponse(infoResponse);

        return response;
    }

    @Override
    public PageInfo<SysUser> page(SysUserRequest request) {
        IPage<SysUser> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        List<SysUser> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            return new PageInfo<>(page.getTotal(), records);
        }
        return null;
    }

    @Override
    public void logout(HttpServletRequest request) {
        tokenUtils.logout(request);
    }

    @Override
    public void register(SysUserRequest request) {
        if (StringUtils.isEmpty(request.getMobile())) {
            throw new WorkException("请输入手机号");
        }
        if (StringUtils.isEmpty(request.getPassword())) {
            throw new WorkException("请输入密码");
        }

        if (StringUtils.isEmpty(request.getNickname())) {
            throw new WorkException("请输入昵称");
        }
        SysUser sysUser = baseMapper.selectOne(new LambdaQueryWrapper<SysUser>().
                eq(SysUser::getMobile, request.getMobile()));
        if (Objects.nonNull(sysUser)) {
            throw new WorkException("该账号已注册");
        }
        SysUser user = new SysUser();
        user.setMobile(request.getMobile());
        user.setAvatar(Constant.DEFAULT_AVATAR);
        user.setPassword(request.getPassword());
        user.setRoleId(1);
        user.setNickname(request.getNickname());
        save(user);
    }

    /**
     * 创建令牌
     *
     * @param sysUser 系统用户
     * @return 令牌
     */
    private String createToken(SysUser sysUser) {
        //设置半个小时
        long expireTime = GeneratorUtil.generateExpireTime(Constant.CONST_ONE_DAY);
        //登录成功创建令牌
        String token = JwtUtil.createSysUserToken(sysUser, expireTime);
        redisUtils.hset(Constant.ADMIN_USER_TOKEN_MAP, sysUser.getId().toString(), "" + expireTime);
        return token;
    }
}
